import { createRouter, createWebHashHistory } from 'vue-router'

const Login = () => import('../components/Login')
const Home = () => import('../components/Home')

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  { path: '/home', component: Home }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach( (to, from, next) => {
  if (to === '/login') next()
  else {
    if (!window.sessionStorage.getItem('token')) {
      router.push('/login')
    } else {
      next()
    }
  }
})

export default router
